//
//  MDSecurityHandler.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 5/6/15.
//  Copyright (c) 2015 Minus. All rights reserved.
//

#import "MDSecurityHandler.h"
#import <MideaHomeSDK/MideaHomeSDK.h>
#import "BaseAPI.h"
#import "MDXPGApplianceApi.h"

#import <java/util/Map.h>
#import <java/util/HashMap.h>
#import <java/util/HashSet.h>

@interface MDSecurityHandler ()
{
    MDDeviceManager *_deviceManager;
}

@end

@implementation MDSecurityHandler

+ (instancetype)sharedInstance
{
    static MDSecurityHandler *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        // 使用 MideaDataSecurity 之前，先 init MDDeviceManager
        [MDXPGApplianceApi deviceManager];
    }
    
    return self;
}

- (NSString *)EncryptWithNSString:(NSString *)content
                     withNSString:(NSString *)appKey
                     withNSString:(NSString *)accessToken
{
    return [MideaDataSecurity sensitiveDataEncryption:accessToken msg:content];
}

- (NSString *)DecryptWithNSString:(NSString *)content
                     withNSString:(NSString *)appKey
                     withNSString:(NSString *)accessToken
{
    return [MideaDataSecurity sensitiveDataDecryption:accessToken reply:content];
}

- (NSString *)RequestSignWithNSString:(NSString *)urlAddr
                         withNSString:(NSString *)apiName
                      withJavaUtilMap:(id<JavaUtilMap>)map
                         withNSString:(NSString *)appKey
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    
    for (id<JavaUtilMap_Entry> set in [map entrySet])
    {
        NSString *key = [set getKey];
        NSString *value = [set getValue];
        
        [params setObject:value forKey:key];
    }
    
    if ([apiName hasPrefix:@"/"])
    {
        apiName = [@"/v1" stringByAppendingString:apiName];
    }
    else
    {
        apiName = [@"/v1/" stringByAppendingString:apiName];
    }
    
    return [MideaDataSecurity getSignWithApi:apiName andPostDic:params];
}

- (NSString *)ResponseSignWithNSString:(NSString *)urlAddr
                          withNSString:(NSString *)apiName
                          withNSString:(NSString *)response
                          withNSString:(NSString *)appKey
{
    return @"";
}

- (NSString *)loginEncryptWithNSString:(NSString *)content
                          withNSString:(NSString *)loginId
                          withNSString:(NSString *)appKey
{
    return [MideaDataSecurity serverLoginPasswordEncryption:content loginID:loginId];
}

- (NSString *)registerEncryptWithNSString:(NSString *)content
{
    return [MideaDataSecurity registerPasswordEncryption:content];
}

@end
